package algorithm.sort;

import org.junit.Test;

/**
 * 二分查找
 *
 * @Author: zhenzhong.wang
 * @Time: 2018/4/9 11:18
 */
public class BinaryChop {

  @Test
  public void main() {
    int source[] = {1, 3, 5, 6, 7, 10, 11, 15, 20, 22, 23, 24, 25, 29, 40, 44};
    // 目标值
    int target = 11;
    int position = -1;

    int low = 0;
    int high = source.length - 1;
    int middle;
    System.out.println(source.length);
    while (low <= high) {
      middle = (high + low) >>> 1;
      if (target < source[middle]) {
        high = middle - 1;
      } else if (target > source[middle]) {
        low = middle + 1;
      } else {
        position = middle;
        break;
      }
    }
    System.out.println("位置：" + position);
  }
}
